package com.li.controller;



import com.li.pojo.Teacher;
import com.li.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

@Controller
public class TeacherController {
    @Resource
    TeacherService service;

    @RequestMapping("/teachers")
    public String list(Model model){
        Collection<Teacher> teachers=service.allTeacher();
        model.addAttribute("teachers",teachers);
        return "teacher/list";
    }
    @GetMapping(value = "/toteacher")
    public String toteacher(Model model){

        return "teacher/add";
    }
    @RequestMapping(value = "/teacher/add")
    //@ResponseBody
    public String addTeacher(Teacher teacher){
        if(service.addTeacher(teacher))return "redirect:/teachers";
        return "教工号重复，添加失败";
    }
    @RequestMapping(value = "/teacher/query")
    public String queryTeacher(){
        return "";
    }

    @GetMapping("/teacher/{tno}")
    public String toupdateTeacher(
            @PathVariable("tno")String tno, Model model){
        Teacher teacher=new Teacher(tno);

        teacher=service.queryTeacher(teacher);
        model.addAttribute("tea",teacher);
        //Collection<Class> classes=studentService.allClass();
       // model.addAttribute("classes",classes);
        return "teacher/update";
    }
    @RequestMapping(value = "/teacher/update")
    public String updateTeacher(Teacher teacher){
        System.out.println("1");
        service.updateTeacher(teacher);
        return "redirect:/teachers";
    }
    @RequestMapping(value = "/deleteacher/{tno}")
    public String deleteTeacher(@PathVariable("tno")String tno){
        Teacher teacher=new Teacher(tno);
        service.deleteTeacher(teacher);
        return "redirect:/teachers";
    }
}
